package cn.ultrarisk.scadmin.manager.web.controller.solr;

import cn.ultrarisk.mybatis.extension.domain.Page;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.dao.ZkBackupDAO;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.dao.ZkNodeDAO;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.entity.ZkBackup;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.entity.ZkNode;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.operator.ZookeeperOperator;
import cn.ultrarisk.scadmin.manager.biz.zookeeper.service.ZkBackupService;
import cn.ultrarisk.scadmin.manager.web.controller.BaseController;
import cn.ultrarisk.scadmin.manager.web.dto.solr.ZKBackupDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ConfigController
 *
 * @author sevendlong(lin23871@163.com)
 * @date 2014/4/17
 * @since V1.0
 */
@Controller
@RequestMapping(value = "/solr/backup")
public class BackupController extends BaseController {

    @Autowired
    private ZkBackupDAO zkBackupDAO;
    @Autowired
    private ZkBackupService zkBackupService;
    @Autowired
    private ZkNodeDAO zkNodeDAO;
    @Autowired
    private ZookeeperOperator zookeeperOperator;

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<ZkBackup>> list(Integer type, Long startTime, Long endTime, int pageNo, int pageSize) {
        Page<ZkBackup> zkBackups = zkBackupDAO.findPage(type, startTime, endTime, pageNo, pageSize);
        return getOkResponseEntity(zkBackups);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/nodes", method = RequestMethod.GET)
    public ResponseEntity<List<ZkNode>> getNodes(@PathVariable("id") Integer id) {
        List<ZkNode> nodes = zkNodeDAO.findByBackupId(id, true, ZkNode.ID, ZkNode.PARENT_ID, ZkNode.PATH);
        return getOkResponseEntity(nodes);
    }

    @ResponseBody
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResponseEntity<String> create(@RequestBody ZKBackupDTO dto) {
        zkBackupService.create(dto.getPath(), dto.getRemark(), ZkBackup.TYPE_MAN);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<String> delete(@PathVariable("id") Integer id) {
        zkBackupService.delete(id);
        return getOkResponseEntity();
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/{nodeId}", method = RequestMethod.GET)
    public ResponseEntity<String> get(@PathVariable("id") Integer id, @PathVariable("nodeId") Integer nodeId) {
        String data = zkNodeDAO.get(nodeId, ZkNode.DATA).getData();
        return getOkResponseEntity(data);
    }

    @ResponseBody
    @RequestMapping(value = "/{id}/download", method = RequestMethod.GET)
    public void download(@PathVariable("id") Integer id) {

    }

    @ResponseBody
    @RequestMapping(value = "/exist", method = RequestMethod.GET)
    public ResponseEntity<Map<String, Boolean>> exist(String path) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("flag", zookeeperOperator.getStat(path) != null);
        return getOkResponseEntity(map);
    }
}
